package com.wu.controller;

import com.wu.config.DelayedQueueConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @program: RabbitMQ-Study
 * @author: 吴磊
 * @create: 2021-12-18 18:06
 * 发送延迟消息
 */
@RestController
@RequestMapping("ttl")
@Slf4j
@Api(tags = "消息发送Controller")
public class SendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ApiOperation("消息发送接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "message", value = "消息内容", dataTypeClass = String.class)
    })
    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable("message") String message) {
        log.info("当前时间：{}，发送一条信息给两个TTL队列：{}", new Date(), message);
        rabbitTemplate.convertAndSend("X", "XA", "消息来自TTL为10s的队列：" + message);
        rabbitTemplate.convertAndSend("X", "XB", "消息来自TTL为40s的队列：" + message);
    }

    /**
     * 开始发消息  TTL
     * 延迟队列优化
     *
     * @param message
     * @param ttlTime
     */
    @ApiOperation("带过期时间的消息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "message", value = "消息内容", dataTypeClass = String.class),
            @ApiImplicitParam(name = "ttlTime", value = "过期时间（ms）", dataTypeClass = String.class)
    })
    @GetMapping("/sendExpirationMsg/{message}/{ttlTime}")
    public void sendMessage(@PathVariable("message") String message, @PathVariable("ttlTime") String ttlTime) {
        log.info("当前时间：{}，发送一条时长为{}毫秒的信息给队列QC：{}", new Date(), ttlTime, message);
        rabbitTemplate.convertAndSend("X", "XC", message, msg -> {
            //设置发送消息的延迟时长
            msg.getMessageProperties().setExpiration(ttlTime);
            return msg;
        });
    }

    /**
     * 基于插件的延迟发送时间
     *
     * @param message
     * @param delayTime
     */
    @ApiOperation("基于插件的延迟发送时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "message", value = "消息内容", dataTypeClass = String.class),
            @ApiImplicitParam(name = "delayTime", value = "延迟时间（ms）", dataTypeClass = Integer.class)
    })
    @GetMapping("/sendDepayMsg/{message}/{delayTime}")
    public void sendDepayMsg(@PathVariable("message") String message, @PathVariable("delayTime") Integer delayTime) {
        log.info("当前时间：{}，发送一条时长为{}毫秒的信息给delayTime队列delayed.queue：{}", new Date(), delayTime, message);
        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME, DelayedQueueConfig.DELAYED_ROUTING_KEY, message, msg -> {
            //设置发送消息的延迟时长
            msg.getMessageProperties().setDelay(delayTime);
            return msg;
        });
    }


}
